package cn.wei.sdd.core.controller.wx;


import cn.wei.common.result.R;
import cn.wei.sdd.base.util.JwtUtils;
import cn.wei.sdd.core.enums.OrderStatusEnum;
import cn.wei.sdd.core.pojo.entity.Orders;
import cn.wei.sdd.core.pojo.vo.ConfirmOrdersVO;
import cn.wei.sdd.core.pojo.vo.PlaceOrdersVO;
import cn.wei.sdd.core.pojo.vo.UserOrdersVO;
import cn.wei.sdd.core.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 回收订单表 前端控制器
 * </p>
 *
 * @author Wei
 * @since 2021-10-25
 */
@RestController
@Api(tags = "用户订单管理")
@RequestMapping("/wxApi/core/order")
public class OrdersController {

    @Autowired
    private OrderService orderService;


    @ApiOperation("回收员确认订单")
    @PostMapping("confirmOrders")
    public R confirmOrders(
            @RequestBody
            @ApiParam(value = "回收员确认订单对象",required = true) ConfirmOrdersVO confirmOrdersVO,
                           HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        orderService.confirmOrders(confirmOrdersVO,userId);
        return R.ok().message("订单确认成功");
    }

    @ApiOperation("预约订单")
    @PostMapping("reservationOrder")
    public R reservationOrder(
            @RequestBody
            @ApiParam(value = "预约订单对象",required = true) PlaceOrdersVO placeOrdersVO,
            HttpServletRequest request
    ){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        orderService.reservationOrder(placeOrdersVO,userId);

        return R.ok().message("预约成功");
    }


    @ApiOperation("根据普通用户id获取所有订单")
    @GetMapping("getOrdersByUserId/{status}")
    public R getOrdersByUserId(
            @ApiParam(value = "订单状态:1(待接单、等待上门)，0（完成、取消）")
            @PathVariable("status") Integer status,
            HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        List<UserOrdersVO> ordersList= orderService.getOrdersByUserId(userId,status);

        return R.ok().data("list",ordersList);
    }


    @ApiOperation("获取待接单的列表")
    @GetMapping("getWaitOrders")
    public R getWaitOrders(){

        List<UserOrdersVO> ordersList = orderService.getWaitOrders();
        return R.ok().data("list",ordersList);
    }

    @ApiOperation("根据回收员id获取我的接单")
    @GetMapping("getOrdersByRecyclerId/{recycler_status}")
    public R getOrdersByRecyclerId(
            @ApiParam("回收员处理订单状态（-1：待回收，0：待打包，1：待验收， 2：完成）")
            @PathVariable("recycler_status") Long recyclerStatus,
            HttpServletRequest request){
        String token = request.getHeader("token");
        Long recyclerId = JwtUtils.getUserId(token);
        List<UserOrdersVO> ordersList = orderService.getOrdersByRecyclerId(recyclerId,recyclerStatus);

        return R.ok().data("list",ordersList);
    }
    @ApiOperation("回收员抢单")
    @GetMapping("grabOrder/{orderNo}")
    public R grabOrder(
            @PathVariable("orderNo") String orderNo,
            HttpServletRequest request){

        String token = request.getHeader("token");
        Long recyclerId = JwtUtils.getUserId(token);
        String recyclerName = JwtUtils.getUserName(token);
        orderService.grabOrder(orderNo,recyclerId,recyclerName);
        return R.ok().message("抢单成功");
    }
    @ApiOperation("获取订单信息")
    @GetMapping("getOrderByOrderNo/{orderNo}")
    public R getOrderByOrderNo(
            @PathVariable("orderNo") String orderNo){
        Orders orders= orderService.getOrderByOrderNo(orderNo);
        return R.ok().data("order",orders);
    }

    @ApiOperation("取消订单")
    @GetMapping("OrderCancelByOrderNo/{orderNo}")
    public R OrderCancelByOrderNo(
            @PathVariable("orderNo") String orderNo,
            HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        int status = orderService.selectOrderStatusByOrderNo(userId,orderNo);
        if (status== OrderStatusEnum.Cancel_Order.getStatus()){
            return R.ok().message("订单已取消");
        }
        else if(status== OrderStatusEnum.Wait_Order.getStatus()){
            orderService.OrderCancelByOrderNo(orderNo);
            return R.ok().message("取消订单完成");
        }
        else if(status== OrderStatusEnum.Waiting_For_The_Door.getStatus()){
            return R.ok().message("订单已接单，无法取消");
        }
        else if(status== OrderStatusEnum.Finish.getStatus()){
            return R.ok().message("订单已完成");
        }
        else if(status== OrderStatusEnum.overTime.getStatus()){
            return R.ok().message("订单已超时");
        }else{
            return R.ok().message("取消订单失败");
        }
    }


}

